// pages/route/setting/setting.js
const http = require("../../../utils/http.js")
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    routeId: '',
    route: {},
    version: '1.0.0'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    http.get("/get")
    .then(res=>{
      this.setData({
        version: res.data,
        routeId: options.routeId
      })
      this.initData();
    })
  },

  initData() {
    let that = this
    let routeId = that.data.routeId
    if (routeId == null) {
      return;
    }
    http.get("/route/getDetail", {
      routeId: routeId
    }).then(res => {
      let routeData = res.data.data;
      let startDayDate = new Date(routeData.route.startDayDate)
      routeData.route.startDayDate = startDayDate.getFullYear() + '.' + startDayDate.getMonth() + '.' + startDayDate.getDate()
      for (let i = 0; i < routeData.routeDay.length; i++) {
        let date = new Date(routeData.routeDay[i].routeDay.dayDate)
        routeData.routeDay[i].routeDay.dayDate = date.getFullYear() + '.' + date.getMonth() + '.' + date.getDate()
      }
      that.setData({
        route: routeData
      })
    })
  },

  updateCover() {
    let that = this
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], //从相册选择
      success: (res) => {
        wx.showLoading({
          title: '上传中...',
        })
        wx.uploadFile({
          url: http.baseURL + '/file/uploadFile',
          filePath: res.tempFilePaths[0],
          name: 'file',
          success(res) {
            let data = JSON.parse(res.data)
            let route = that.data.route
            route.route.cover = data.data.fileUrl
            that.setData({
              route: route
            })
            wx.hideLoading();
          }
        })
      }
    });
  },

  inputchange(e) {
    let that = this
    let route = that.data.route
    route.route.title = e.detail.value
    that.setData({
      route: route
    })
  },

  switchchange(e) {
    let that = this
    let route = that.data.route
    if (e.detail.value) {
      route.route.visibleStatus = 1
    } else {
      route.route.visibleStatus = 0
    }
    that.setData({
      route: route
    })
  },

  save() {
    let that = this
    let route = that.data.route
    let param = {
      id: route.route.id,
      title: route.route.title,
      visibleStatus: route.route.visibleStatus,
      cover: route.route.cover
    }
    http.post("/route/update", param)
      .then(res => {
        if (res.data.status == 200) {
          wx.navigateBack({
            delta: 1
          })
        }
      })
  },

  deleteRoute() {


    let that = this

    wx.showModal({
      title: '提示',
      content: '真的要删除该行程吗？',
      success(res) {
        if (res.confirm) {
          let route = that.data.route
          http.get("/route/delete", {
            id: route.route.id
          })
            .then(res => {
              if (res.data.status == 200) {
                wx.reLaunch({
                  url: "/pages/index/index"
                })
              }
            })
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})